package com.mrzhou.auth.service;

import com.mrzhou.auth.constant.AuthConstant;
import com.mrzhou.core.tool.constants.CloudConstant;
import com.mrzhou.data.util.CacheUtil;
import lombok.SneakyThrows;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

/**
 * 配置客户端信息
 */
@Component
public class CloudClientDetailsServiceImpl extends JdbcClientDetailsService {

    public CloudClientDetailsServiceImpl(DataSource dataSource) {
        super(dataSource);
    }

    /**
     * 获取客户端信息
     * 这里未设置超时时间，需在更新客户端信息时， 清空缓存
     */
    @Override
    @SneakyThrows
    public ClientDetails loadClientByClientId(String clientId) {
        return CacheUtil.get(CloudConstant.AUTH_CACHE, AuthConstant.REDIS_PREFIX_CLIENT_INFO,
                clientId, BaseClientDetails.class,
                () -> (BaseClientDetails) super.loadClientByClientId(clientId));
    }

}
